#!/usr/bin/env python
# coding=utf-8

# File Name: updateID.py
# Author: Bill Zhang
# Mail: zchcandid@gmail.com 
# Created Time: Thu 14 Nov 2013 03:00:42 PM CST
#python3 compatible
from __future__ import unicode_literals  
from __future__ import print_function 
#tornado server
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options

#json 
import json

define("port", default=6688, help="run on the given port", type=int)
class updateID(tornado.web.RequestHandler):
  #accept the json data from icmall.com 
  #json formate:{"item_id":"[1243,244]","priority":"1"}
    def post(self):
        ToUpdate= self.get_argument('json','nodata')
        if(ToUpdate=='nodata'):
            self.write('something wrong!! nodata input!!!')
        #procesing the data    
        else:
            #self.write('hello,Xiongbin,I get your message!')
            item_id='item_id'
            priority='priority'
            print(ToUpdate)
            IDs=json.loads(ToUpdate)
            print(IDs.keys())
            print(len(IDs[item_id]))
            print(IDs[item_id])
            print(IDs[item_id][0])
            print(IDs[priority])
if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(handlers=[(r"/", updateID)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    print("The web service is runing")
    tornado.ioloop.IOLoop.instance().start()
